/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.aseclipse.core.runtime.registry.classes {

import org.aseclipse.core.runtime.registry.IExtension;


/**
 * An object which represents the user-defined extension in a 
 * plug-in manifest.  
 * <p>
 * This class may be instantiated, or further subclassed.
 * </p>
 */
public class Extension extends RegistryModelObject implements IExtension {

	// DTD properties (included in plug-in manifest)
	private var extensionPoint:String;
	private var id:String;
	private var elements: Array; //IConfigurationElement [];
	// this extension's elements data offset in the registry cache
	private var subElementsCacheOffset:int;
	// is this extension already fully loaded?
	private var fullyLoaded:Boolean = true;

	/**
	 * Two Extensions are equal if they have the same Id
	 * and target the same extension point.
	 */
	public function equals(object:Object):Boolean {
		if (object is Extension) {
			var em:Extension = object as Extension;
			return (id == em.id) && (extensionPoint == em.extensionPoint);
		}
		return false;
	}

	/**
	 * Returns the extension point with which this extension is associated.
	 *
	 * @return the extension point with which this extension is associated
	 *  or <code>null</code>
	 */
	public function getExtensionPointIdentifier():String {
		return extensionPoint;
	}
	/**
	 * Returns the simple identifier of this extension, or <code>null</code>
	 * if this extension does not have an identifier.
	 * This identifier is specified in the plug-in manifest as a non-empty
	 * string containing no period characters (<code>'.'</code>) and 
	 * must be unique within the defining plug-in.
	 *
	 * @return the simple identifier of the extension (e.g. <code>"main"</code>)
	 *  or <code>null</code>
	 */
	public function getSimpleIdentifier():String {
		return id;
	}
	public function getUniqueIdentifier():String {
		return id == null ? null : this.getParentIdentifier() + "." + id; //$NON-NLS-1$
	}
	
	public function getParentIdentifier():String {
		var parent:BundleModel = this.getParent() as BundleModel;
		return parent.isFragment() ? parent.getHostIdentifier() : parent.getName();
	}
	public function getConfigurationElements():Array { //IConfigurationElement[] {
		return elements;
	}

	/**
	 * Sets this model object and all of its descendents to be read-only.
	 * Subclasses may extend this implementation.
	 *
	 * @see #isReadOnly
	 */
	override public function markReadOnly():void {
		super.markReadOnly();
		if (elements != null)
			for (var i:int = 0; i < elements.length; i++)
				 (elements[i] as ConfigurationElement).markReadOnly();
	}
	/**
	 * Set the extension point with which this extension is associated.
	 * This object must not be read-only.
	 *
	 * @return the extension point with which this extension is associated.  
	 *		May be <code>null</code>.
	 */
	public function setExtensionPointIdentifier(value:String):void {
		assertIsWriteable();
		extensionPoint = value;
	}
	/**
	 * Sets the simple identifier of this extension, or <code>null</code>
	 * if this extension does not have an identifier.
	 * This identifier is specified in the plug-in manifest as a non-empty
	 * string containing no period characters (<code>'.'</code>) and 
	 * must be unique within the defining plug-in.
	 * This object must not be read-only.
	 *
	 * @param value the simple identifier of the extension (e.g. <code>"main"</code>).
	 *		May be <code>null</code>.
	 */
	public function setSimpleIdentifier(value:String):void {
		assertIsWriteable();
		id = value;
	}
	/**
	 * Sets the configuration element children of this extension.
	 * This object must not be read-only.
	 *
	 * @param value the configuration elements in this extension.  
	 *		May be <code>null</code>.
	 */
	public function setSubElements(value:Array):void {
		assertIsWriteable();
		elements = value;
	}
	public function getLabel():String {
		return getName();
		//if (s == null)
		//	return ""; //$NON-NLS-1$
		//return ((BundleModel) getParent()).getResourceString(s);
	}
	override public function toString():String {
		return getUniqueIdentifier() + " -> " + getExtensionPointIdentifier(); //$NON-NLS-1$
	}

	protected function setSubElementsCacheOffset(value:int):void {
		subElementsCacheOffset = value;
	}
	public function isFullyLoaded():Boolean {
		return fullyLoaded;
	}
	public function setFullyLoaded(value:Boolean):void {
		fullyLoaded = value;
	}

}
}